<!--

    Copyright © 2016-2019 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section flex layout='column' layout-align="center" layout-gt-sm='row' layout-align-gt-sm="start center">
	   <tb-entity-alias-select
							  tb-required="true"
							  alias-controller="aliasController"
							  ng-model="entityAlias"
							  on-create-entity-alias="onCreateEntityAlias({event: event, alias: alias})">
	   </tb-entity-alias-select>
	   <section flex layout='column'>
		   <section flex layout='column' layout-align="center" style="padding-left: 4px;">
			   <md-chips flex ng-if="widgetType != types.widgetType.alarm.value"
                         id="datakey_chips"
                         ng-model="dataKeys"
                         md-transform-chip="transformDataKeyChip($chip)"
                         md-require-match="false">
					  <md-autocomplete
							md-no-cache="true"
							id="datakey"
							md-selected-item="selectedDataKey"
							md-search-text="dataKeySearchText"
							md-items="item in dataKeysSearch(dataKeySearchText)"
							md-item-text="item.name"
							md-min-length="0"
							placeholder=""
							md-menu-class="tb-datakey-autocomplete">
							<span ng-show="item.type==types.dataKeyType.attribute">
								<md-tooltip>{{'datakey.attributes' | translate }}</md-tooltip>
								<ng-md-icon size="16" icon="alpha-a-circle-outline"></ng-md-icon>
							</span>
							<span ng-show="item.type==types.dataKeyType.timeseries">
								<md-tooltip>{{'datakey.timeseries' | translate }}</md-tooltip>
								<ng-md-icon size="16" icon="timeline"></ng-md-icon>
							</span>
                            <span md-highlight-text="dataKeySearchText" md-highlight-flags="^i">{{item.name}}</span>
						    <md-not-found>
							  <div class="tb-not-found">
								  <div class="tb-no-entries" ng-if="!textIsNotEmpty(dataKeySearchText)">
									  <span translate>entity.no-keys-found</span>
								  </div>
								  <div ng-if="textIsNotEmpty(dataKeySearchText)">
									  <span translate translate-values='{ key: "{{dataKeySearchText | truncate:true:6:&apos;...&apos;}}" }'>entity.no-key-matching</span>
									  <span>{{'entity.create-new-key' | translate }}  </span>
									  <span ng-show="widgetType == types.widgetType.latest.value">
										  <md-tooltip>{{'datakey.attributes' | translate }}</md-tooltip>
									      <ng-md-icon size="16" icon="alpha-a-circle-outline" ng-click="createKey($event, types.dataKeyType.attribute, '#datakey_chips')"></ng-md-icon>
									  </span>
			                          <span>
										  <md-tooltip>{{'datakey.timeseries' | translate }}</md-tooltip>
			                              <ng-md-icon size="16" icon="timeline" ng-click="createKey($event, types.dataKeyType.timeseries, '#datakey_chips')"></ng-md-icon>
			                          </span>
								  </div>
							  </div>
						    </md-not-found>
					  </md-autocomplete>
					  <md-chip-template>
						  <div layout="row" layout-align="start center" class="tb-attribute-chip">
                              <div class="tb-chip-drag-handle" style="margin-right: 5px;" tb-chip-draggable>
                                  <ng-md-icon size="20" icon="drag_handle"></ng-md-icon>
                              </div>
							  <div class="tb-color-preview" ng-click="showColorPicker($event, $chip, $index)" style="margin-right: 5px;">
								  <div class="tb-color-result" ng-style="{background: $chip.color}"></div>
							  </div>
							  <div layout="row">
								  <div class="tb-chip-label">
                                      <span ng-show="$chip.type==types.dataKeyType.attribute">
                                          <md-tooltip>{{'datakey.attributes' | translate }}</md-tooltip>
                                          <ng-md-icon size="20" icon="alpha-a-circle-outline"></ng-md-icon>
                                      </span>
                                      <span ng-show="$chip.type==types.dataKeyType.timeseries">
                                          <md-tooltip>{{'datakey.timeseries' | translate }}</md-tooltip>
                                          <ng-md-icon size="20" icon="timeline"></ng-md-icon>
                                      </span>
                                          {{$chip.label}}
								  </div>
								  <div class="tb-chip-separator">: </div>
								  <div class="tb-chip-label">
									  <strong ng-if="!$chip.postFuncBody">{{$chip.name}}</strong>
									  <strong ng-if="$chip.postFuncBody">f({{$chip.name}})</strong>
								  </div>
							  </div>
							  <md-button ng-click="editDataKey($event, $chip, $index)" class="md-icon-button tb-md-32">
								  <md-icon aria-label="edit" class="material-icons tb-md-20">edit</md-icon>
							  </md-button>
						  </div>
					  </md-chip-template>
			   </md-chips>
			   <md-chips flex ng-if="widgetType == types.widgetType.alarm.value"
						 id="alarm_datakey_chips"
						 ng-required="true"
						 ng-model="alarmDataKeys"
						 md-transform-chip="transformAlarmDataKeyChip($chip)"
						 md-require-match="false">
				   <md-autocomplete
						   md-no-cache="true"
						   id="alarm_datakey"
						   md-selected-item="selectedAlarmDataKey"
						   md-search-text="alarmDataKeySearchText"
						   md-items="item in dataKeysSearch(alarmDataKeySearchText)"
						   md-item-text="item.name"
						   md-min-length="0"
						   placeholder="{{'datakey.alarm' | translate }}"
						   md-menu-class="tb-alarm-datakey-autocomplete">
					   <span md-highlight-text="alarmDataKeySearchText" md-highlight-flags="^i">{{item}}</span>
					   <md-not-found>
						   <div class="tb-not-found">
							   <div class="tb-no-entries" ng-if="!textIsNotEmpty(alarmDataKeySearchText)">
								   <span translate>entity.no-keys-found</span>
							   </div>
							   <div ng-if="textIsNotEmpty(alarmDataKeySearchText)">
								   <span translate translate-values='{ key: "{{alarmDataKeySearchText | truncate:true:6:&apos;...&apos;}}" }'>entity.no-key-matching</span>
								   <span>
										<a translate ng-click="createKey($event, '#alarm_datakey_chips')">entity.create-new-key</a>
								   </span>
							   </div>
						   </div>
					   </md-not-found>
				   </md-autocomplete>
				   <md-chip-template>
					   <div layout="row" layout-align="start center" class="tb-attribute-chip">
                           <div class="tb-chip-drag-handle" style="margin-right: 5px;" tb-chip-draggable>
                               <ng-md-icon size="20" icon="drag_handle"></ng-md-icon>
                           </div>
						   <div class="tb-color-preview" ng-click="showColorPicker($event, $chip, $index)" style="margin-right: 5px;">
							   <div class="tb-color-result" ng-style="{background: $chip.color}"></div>
						   </div>
						   <div layout="row">
							   <div class="tb-chip-label">
								   {{$chip.label}}
							   </div>
							   <div class="tb-chip-separator">: </div>
							   <div class="tb-chip-label">
								   <strong ng-if="!$chip.postFuncBody">{{$chip.name}}</strong>
								   <strong ng-if="$chip.postFuncBody">f({{$chip.name}})</strong>
							   </div>
						   </div>
						   <md-button ng-click="editDataKey($event, $chip, $index)" class="md-icon-button tb-md-32">
							   <md-icon aria-label="edit" class="material-icons tb-md-20">edit</md-icon>
						   </md-button>
					   </div>
				   </md-chip-template>
			   </md-chips>
		   </section>
		   <div class="tb-error-messages" ng-messages="ngModelCtrl.$error" role="alert">
			    <div translate ng-message="entityKeys" ng-if="widgetType === types.widgetType.timeseries.value" class="tb-error-message">datakey.timeseries-required</div>
				<div translate ng-message="entityKeys" ng-if="widgetType === types.widgetType.latest.value" class="tb-error-message">datakey.timeseries-or-attributes-required</div>
			    <div translate ng-message="entityKeys" ng-if="widgetType === types.widgetType.alarm.value" class="tb-error-message">datakey.alarm-fields-required</div>
			</div>
		   <div class="md-caption" style="color: rgba(0,0,0,0.57);" ng-if="maxDataKeys > -1"
				translate="datakey.maximum-timeseries-or-attributes"
				translate-values="{count: maxDataKeys}"
				translate-interpolation="messageformat"
		   ></div>
	   </section>
</section>
